<template>
  <div class="wrapper">
    <el-form>
      <el-form-item label="选择图片">
        <input type="file" placeholder="请选择图片" ref="file" @change="fileChange"/>
        <el-input type="file" v-model="filePath" placeholder="请选择图片" ref="file2" @change="fileChange2"/>
      </el-form-item>
    </el-form>
    <el-row>
      <span>{{file}}----{{file.name}}</span>
    </el-row>
    <el-row>
      <img :alt="imageSrc" :src="imageSrc"/>
    </el-row>
    <el-button @click="upload" :loading="loading">上传图片</el-button>
    <el-button @click="fresh">刷新</el-button>
    <el-button @click="show2">查看文件二</el-button>
    <el-progress :percentage="progress"/>
  </div>
</template>

<script>
import * as qiniu from 'qiniu-js'

export default {
  name: 'AppTest',
  components: {},
  data () {
    return {
      file: '',
      filePath: '',
      qiniu: qiniu,
      loading: false,
      progress: 10,
      imageSrc: '',
      token: 'K0-mYOpGusytx_JLH2X2zE2wzfOVVhxJttOsdy_j:oWTKye298TDGp0bYa2_S9GQfDY8=:eyJjYWxsYmFja0JvZHlUeXBlIjoiYXBwbGljYXRpb24vanNvbiIsInNjb3BlIjoieHVtZGEtY29tbWVyY2U6dGVzdEZpbGUucG5nIiwiY2FsbGJhY2tVcmwiOiIxMDYuMTMuMTY4LjE0OjIwMDAxL3VzZXJJbmZvL2hlYWRJbWFnZVVwbG9hZENhbGxiYWNrIiwiZGVhZGxpbmUiOjE2MTUzMzkzNjUsImNhbGxiYWNrQm9keSI6IntcInVzZXJJZFwiOlwibGl4ZFwiLFwia2V5XCI6XCIkKGtleSlcIixcImJ1Y2tldFwiOlwiJChidWNrZXQpXCIsXCJoYXNoXCI6XCIkKGV0YWcpXCIsXCJmbmFtZVwiOlwiJChmbmFtZSlcIixcImZzaXplXCI6XCIkKGZzaXplKVwiLFwibWltZVR5cGVcIjpcIiQobWltZVR5cGUpXCIsXCJleHRcIjpcIiQoZXh0KVwiLFwiZnByZWZpeFwiOlwiJChmcHJlZml4KVwiLFwiaGFzaFwiOlwiJChoYXNoKVwiLFwicGFyYW0xXCI6XCIkKHg6cGFyYW0xKVwiLFwicGFyYW0yXCI6XCIkKHg6cGFyYW0yKVwiLFwicGFyYW0yXCI6JCh4OnBhcmFtMyl9In0='
    }
  },
  methods: {
    fresh () {
    },
    fileChange (e) {
      this.file = e.target.files[0]
    },
    fileChange2 () {
      this.$message.warning('file2change')
      this.file = this.$refs.file2.$el.firstElementChild.files[0]
    },
    show2 (e) {
    },
    upload () {
      const vm = this
      if (!this.token) {
        this.$message.warning('token不能为空')
        return
      }
      if (!this.file) {
        this.$message.warning('请先选择上传文件')
        return
      }
      if (this.token && this.file) {
        vm.loading = true
        const config = {
          useCdnDomain: true,
          region: qiniu.region.z2
        }
        const putExtra = {
          params: {
            // 这里配置自定义名称
            'x:param1': 11
          }
        }
        const observable = qiniu.upload(this.file, 'testFile.png', this.token, putExtra, config)
        observable.subscribe({
          next (res) {
            vm.progress = Math.ceil(res.total.percent)
          },
          error (res) {
            vm.$message.error('上传文件失败: code=' + res.code + '\tmessage=' + res.message + '\treqId' + res.reqId + '\tisRequestError' + res.isRequestError)
            vm.loading = false
          },
          complete (res) {
            if (res && res.code && res.code === 200) {
              vm.imageSrc = 'http://commerce.qiniu.lixunda.top/' + res.object.key
              vm.$message.success('上传文件成功:' + JSON.stringify(res))
            }
            vm.loading = false
          }
        })
      }
    }
  },
  watch: {
    file () {
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
